package de.kimera.controller;

import java.awt.Cursor;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.JDialog;
import javax.swing.JProgressBar;

import de.kimera.api.IEmail;
import de.kimera.view.ApplicationFrame;

public class EmailManager extends Thread{
	
	private JDialog sendEmailDialog;
	private JProgressBar progressBar;
	private IEmail email;
	
	public EmailManager(JDialog sendEmailDialog, JProgressBar progressBar, IEmail email){
		
		this.sendEmailDialog = sendEmailDialog;
		this.progressBar = progressBar;
		this.email = email;
	}
	
	 @Override
	 public void run() {
		 send();
	 }
	
	private void send(){
		try{
			//modify mouse cursor
			ApplicationFrame.frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
			sendEmailDialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
			
			String host="smtp.gmail.com";
			int port=587;
       	        
			Properties props=new Properties();
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.starttls.enable", "true");
        
    		progressBar.setValue(20);
        
			Session session=Session.getInstance(props);
			Transport transport=session.getTransport("smtp");
			transport.connect(host, port, email.getFrom(), email.getPassword());
        
	        Address[] toAddresses=InternetAddress.parse(email.getTo());
	        Address[] ccAddresses=InternetAddress.parse(email.getCc());
	        
		    progressBar.setValue(50);
        
	        Message message=new MimeMessage(session);
	        message.setFrom(new InternetAddress(email.getFrom()));
	        message.setRecipients(Message.RecipientType.TO, toAddresses);
	        message.setRecipients(Message.RecipientType.CC, ccAddresses);
	        message.setSubject(email.getSubject());
        
	        MimeBodyPart mbp1 = new MimeBodyPart();
	        mbp1.setText(email.getText());

	        // create the second message part
	        MimeBodyPart mbp2 = new MimeBodyPart();

	        // attach the file to the message
	        mbp2.attachFile(email.getPdfAttachment());
        
	        Multipart multipart = new MimeMultipart();
	        multipart.addBodyPart(mbp1);
	        multipart.addBodyPart(mbp2);

	        // add the Multipart to the message
	        message.setContent(multipart);
	    
	 	    progressBar.setValue(80);
    		
	 	    transport.sendMessage(message, message.getAllRecipients());
     		
	 	    transport.close();
	        progressBar.setValue(100);
	        
	        sendEmailDialog.dispose();
	        
	        //modify mouse cursor back to the default cursor
	        ApplicationFrame.frame.setCursor(Cursor.getDefaultCursor());
	        
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}
